package Administrator.Login.AdministratorLoginModel;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.io.*;
import java.net.*;

import Administrator.Login.AdministratorLoginView.*;
import Administrator.Login.AdministratorLoginModel.*;
import Administrator.Login.AdministratorLoginController.*;

import Core.Model.*;

import Exception.LoginFailureException;
import Exception.SocketConnectionFailureException;
import Packet.Packet;
import Packet.PacketFactory;

public class AdministratorLoginModel 
{
	public void initialize()
	{
		initializeSeverIp();
	}

	public void initializeSeverIp()
	{
		try
		{
			BufferedReader in = new BufferedReader(new FileReader(this.ConfigureFilePath));
			serverIp = in.readLine();
			port = new Integer(in.readLine());
		}
		catch(FileNotFoundException exception)
		{
			System.out.println("FileNotFound : " + this.ConfigureFilePath);
			exception.printStackTrace();
		}
		catch(IOException exception)
		{
			System.out.println("IOException when read file : " + this.ConfigureFilePath);	
			exception.printStackTrace();
		}
		catch(Exception exception)
		{
			System.out.println("Exception happened when initializeServerIp");
			exception.printStackTrace();
		}
	}

	public void setViewAndController(AdministratorLoginView view,
			AdministratorLoginController controller) 
	{
		this.view = view;
		this.controller = controller;
	}

	public boolean login(String userName, char[] password) throws SocketConnectionFailureException, LoginFailureException 
	{
		try 
		{
			if (initializeSocket() == true) 
			{
				Packet packet = new PacketFactory().makeClientLoginPacket(PacketFactory.sourceId,
						PacketFactory.destinationId, "", "", ClientType.TEAM);

				try
				{
					ObjectOutputStream objectOutputStream = new 
					ObjectOutputStream(this.socket.getOutputStream());

					objectOutputStream.writeObject(packet);
				}
				catch(IOException exception)
				{
					exception.printStackTrace();
				}


				ServerConnection serverConnection = new ServerConnection(socket, ClientType.ADMINISTRATOR);
				try 
				{
					if (serverConnection.login(userName, new String(password))) 
					{
						return true;
					}
				} 
				catch (LoginFailureException exception) 
				{
					throw exception;
				}
			}
		} 
		catch (SocketConnectionFailureException exception)
		{
			throw exception;
		}
		return true;
	}

	public boolean initializeSocket() throws SocketConnectionFailureException
	{
		try
		{
			socket = new Socket(serverIp, port);
			System.out.println("team socket has been created!");
		}
		catch(IOException exception)
		{
			System.out.println("IOException happened when initialize the Socket!");
			exception.printStackTrace();
			throw new SocketConnectionFailureException("Cann't connect to server, Please contact the staff!");
		}
		return true;
	}

	private AdministratorLoginView view;

	private AdministratorLoginController controller;


	private Socket socket;

	private String serverIp;
	private int port;
	private String ConfigureFilePath = new String("c:\\server_process_address.txt"); 
}
